clear all
clear matrix
version 13
capture log close
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
	global main		= "\\iab.baintern.de\DFS\017\Ablagen\D01700-Data\N2066017\Datenaustausch\GradAB\Schnitzlein_Stueber\Stueber\CADAL_Flow_Dataset\Federica"
	global data_orig= "N:\Ablagen\D01700-IAB-Projekte\D01700-CADAL_Flow_Dataset\package"
	global data_proc= "$main"+"\"+"temp"
	global log   	= "$main"+"\"+"log"
	global do		= "$main"+"\"+"do"
	global data_add = "$main"+"\"+"data"
*-------------------------------------------------------------------------------
cd "$main"
*-------------------------------------------------------------------------------
********************************************************************************
* INPUT: RAW DATA
* OUTPUT: GET QUARTERLY LOCAL CONCENTRATION TIME SERIES
********************************************************************************
use "$data_proc\data_federica_1990_2014_west.dta", clear 

gen quarter = q - 4 * (a - 1)
label var a "index of year"
label var year "year"
label var st_reg "stock regular workers"
label var founded_q "Quarter in which establishment founded"
label var quarter "Quarter (1-4)"
label var district "district (Kreis) of establishment"

rename st_reg employment

* Count of (new) establishments:
gen count_all = 1
gen count_new = 0
replace count_new = 1 if q == founded_q

* Create time variable:
gen eventdate=yq(year,quarter)
format eventdate %tq
drop year quarter

rename w93_imp w93_3
sum employment 
count if employment==.
count if employment==.z
********************************************************************************
* Drop public sector & education 
drop if w93_3>=750 & w93_3<900
********************************************************************************
* Drop agriculture and mining:
drop if w93_3<150
********************************************************************************
* Get info on MAs:
qui: ren district krs
qui: destring krs, force replace

merge m:1 krs using "$data_add/amr_krs_72.dta"
drop if _merge!=3
drop _merge
********************************************************************************
* Get percentiles:
egen id =group(amr eventdate)

preserve
keep id amr eventdate
duplicates drop
ren amr id_geo
saveold "$data_proc/id_cxwalk.dta", replace
restore

preserve
collapse (p90) employment, by(id)
ren employment e_p90
saveold "$data_proc/employmentp90.dta", replace
restore

preserve
collapse (p95) employment, by(id)
ren employment e_p95
saveold "$data_proc/employmentp95.dta", replace
restore

preserve
collapse (p99) employment, by(id)
ren employment e_p99
saveold "$data_proc/employmentp99.dta", replace
restore

by id, sort: egen totemployment = total(employment)

merge m:1 id using "$data_proc/employmentp90.dta"
drop if _merge!=3
drop _merge

gen above = 0
replace above = 1 if employment>e_p90
by id, sort: egen temp = total(employment*above)
gen share_p90 = temp/totemployment
drop temp above

merge m:1 id using "$data_proc/employmentp95.dta"
drop if _merge!=3
drop _merge

gen above = 0
replace above = 1 if employment>e_p95
by id, sort: egen temp = total(employment*above)
gen share_p95 = temp/totemployment
drop temp above

merge m:1 id using "$data_proc/employmentp99.dta"
drop if _merge!=3
drop _merge

gen above = 0
replace above = 1 if employment>e_p99
by id, sort: egen temp = total(employment*above)
gen share_p99 = temp/totemployment
drop temp above

gen sharei2 = (employment/totemployment)^2
by eventdate amr, sort: egen herfindahl = sum(sharei2)

keep share_* herfindahl amr eventdate id
duplicates drop

saveold "$data_proc/qoq_percentiles_72.dta", replace version(14)
********************************************************************************
